package com.baichang.taoshunda.service.impl;

import com.baichang.taoshunda.dao.FavoritesDao;
import com.baichang.taoshunda.dao.UserDao;
import com.baichang.taoshunda.dao.WaiterListDao;
import com.baichang.taoshunda.entity.User;
import com.baichang.taoshunda.service.FavoritesService;
import com.baichang.taoshunda.service.WaiterService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2017/8/18 0018.
 */
@Service
public class WaiterServiceImpl implements WaiterService {
    @Autowired
    private WaiterListDao waiterListDao;
    @Autowired
    private FavoritesService favoritesService;
    @Autowired
    private FavoritesDao favoritesDao;
    @Autowired
    private UserDao userDao;

    /**
     * 根据类型编号获取本地服务列表
     *
     * @param typeId   类型编号
     * @param cityId   城市编号
     * @param ulat
     * @param ulng     @return
     * @param nowPage
     * @param pageSize
     * @param userId
     * @param areaId
     */
    @Override
    public List<Map<String, Object>> getwaiterList(int typeId, int cityId, double ulat, double ulng, int nowPage, int pageSize, int userId, int areaId) {
        List<Map<String, Object>> list = waiterListDao.getwaiterList(typeId, cityId, areaId, ulat, ulng, nowPage, pageSize);
        try {

            for (int i = 0; i < list.size(); i++) {
                list.get(i).put("isFavorites", favoritesService.getCountByUserAndBusId(Integer.parseInt(list.get(i).get("id").toString()), userId, 2));
            }

            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return list;
        }

    }

    @Override
    public Map<String, Object> getWaiterByid(Integer id) {
        Map<String, Object> map = waiterListDao.getIdInfo(id);
//        WaiterList waiterList = waiterListDao.getEntityById(id);
//        if (waiterList != null) {
//            map = waiterList.toMap();
//        }
        return map;
    }

    /**
     * 服务商家详情
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> getWaiterDetail(Integer id, int userId) {
        Map<String, Object> map = waiterListDao.getIdInfo(id);
        if (map != null && !map.isEmpty()) {
            Map<String, Object> maps = favoritesDao.queryWaiterBusienss(id, userId);
            map.put("isFavorites", maps.get("num"));
            User user = userDao.getEntityById(Integer.parseInt(map.get("userId").toString()));
            map.put("userName", user.getNickName());
            map.put("ryToken", JSONObject.fromObject(user.getRyToken()).get("token"));
            map.put("ryUserId", JSONObject.fromObject(user.getRyToken()).get("userId"));
        } else {
            map = new HashMap(1);
            map.put("status", 0);

        }
        return map;
    }

    @Override
    public int getWaiterCount(Integer id) {
        return waiterListDao.getWaiterCount(id);
    }
}